// 初始化
function applyCollect() {
    $(".form-collect").jQValidator({
        isClearForm: false,
        excluded: ':disabled',
        fields: {
            applySuggest: {
                validators: {
                    notEmpty: {
                        message: '此项为必填项'
                    }
                }
            },
            applyTel: {
                validators: {
                    notEmpty: {
                        message: '请填写您的手机号'
                    },
                    phone: {
                        phoneType: 'MB'
                    }
                }
            },
            applyName: {
                validators: {
                    notEmpty: {
                        message: '请填写您的姓名'
                    },
                }
            },
            applyWork: {
                validators: {
                    notEmpty: {
                        message: '请填写您的工作单位'
                    },
                }
            },
            applyVcode: {
                validators: {
                    notEmpty: {
                        message: '请填写验证码'
                    },
                }
            },
        }
    });
}

$('.btn-hrapply').on(tap, function() {
    $('#modalHrbox').modal('show');
});
// $('#modalHrbox').on('shown.bs.modal', function() {
//     applyForm();
// })
$(window).on("load", function() {
    applyCollect();
});

$('.form-collect .form-control').change(function() {
    var isValid;
    var jQValidator = $(".form-collect").data('jQValidator');
    jQValidator.validate();

    /// bool, 所有字段验证是否正确
    var isValid = $('.form-collect').data('jQValidator').isValid();

    // $("body").addClass('green1')

    if (isValid) {
        $("body").addClass('green1')
        var params = {
            menuId: menuid,
            sourceMenuId: menuid,
            relevanceId: relevanceId,
            relevanceTitle: title,
            name: $("input[name='applyName']").val(),
            phone: $("input[name='applyTel']").val(),
            companyName: $("input[name='applyWork']").val(),
            intro: $("#applySuggest").val(),
            valiCode: $("input[name='applyVcode']").val()
        };

    } else {
        $("body").removeClass('green1')
    }
})


// 表单提交
$('.form-collect .btn-submit').click(function() {
    /// 点击提交按钮进行校验
    var isValid;
    var jQValidator = $(".form-collect").data('jQValidator');
    jQValidator.validate();

    /// bool, 所有字段验证是否正确
    var isValid = $('.form-collect').data('jQValidator').isValid();

    // $("body").addClass('green1')

    if (isValid) {
        var params = {
            menuId: menuid,
            sourceMenuId: menuid,
            relevanceId: relevanceId,
            relevanceTitle: title,
            name: $("input[name='applyName']").val(),
            phone: $("input[name='applyTel']").val(),
            companyName: $("input[name='applyWork']").val(),
            intro: $("#applySuggest").val(),
            valiCode: $("input[name='applyVcode']").val()
        };

        $('.p-collect .animbox-box').show()
        abp.services.front.messagesFront
            .registerMessages(params, {
                async: false,
                timeout: 30000,
                headers: {
                    'f_tenant_id': abp.httpCache.tenantId,
                    'f_culture': abp.httpCache.cultureName
                }
            })
            .done(function(data) {
                $('.p-collect .animbox-box').hide();
                if (data == 1) {

                    $('.p-collect #modalTipsSuc').show()
                    $('.p-collect #modalTipsSuc').css('opacity', '1')
                } else if (data == 4) {
                    resetCode();
                    $("#errTxt").html("验证码错误")
                    $('.p-collect #modalTipsError').show()
                    $('.p-collect #modalTipsError').css('opacity', '1')
                } else if (data == 5) {
                    resetCode();
                    $("#errTxt").html("本期调查征集已结束，谢谢参与！")
                    $('.p-collect #modalTipsError').show()
                    $('.p-collect #modalTipsError').css('opacity', '1')
                } else {
                    resetCode();
                    $("#errTxt").html("")
                    $('.p-collect #modalTipsError').show()
                    $('.p-collect #modalTipsError').css('opacity', '1')
                }
            })

    }



    // 表单关闭
    $('.has-error .btn-sure').on('click', function() {
        $(this).parents('.modal-tips').hide()
        $(this).parents('.modal-tips').css('opacity', '0')
        $('.p-collect .animbox').hide()
    })

    // 表单关闭
    $('.has-success .btn-sure').on('click', function() {
        $(this).parents('.modal-tips').hide()
        $(this).parents('.modal-tips').css('opacity', '0')
        $('.p-collect .animbox').hide()
        location.reload();
    })
});